package com.hxzy.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.vo.R;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes;

public class ServletUtil {

    /**
     * 向客户端发送json数据 (以后不用自己写servletUtil工具了)
     * @param response
     * @param ackCode
     */
    public static void outJson(HttpServletResponse response, AckCode ackCode){
        response.setCharacterEncoding("UTF-8");
        R r= R.build(ackCode);
        //r--> json
        String jsonStr= JSONUtil.toJsonStr(r);
        cn.hutool.extra.servlet.ServletUtil.write(response,jsonStr, MediaType.APPLICATION_JSON_VALUE);
    }

    /**
     * 向客户端发送json数据 (以后不用自己写servletUtil工具了)
     * @param response
     * @param r
     */
    public static void outJson(HttpServletResponse response, R r){
        response.setCharacterEncoding("UTF-8");
        String jsonStr= JSONUtil.toJsonStr(r);
        cn.hutool.extra.servlet.ServletUtil.write(response,jsonStr, MediaType.APPLICATION_JSON_VALUE);
    }

    /**
     * 向客户端发送json数据 (以后不用自己写servletUtil工具了)
     * @param ackCode
     */
    public static void outJson( AckCode ackCode){
        //绑定当前请求线程
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        response.setCharacterEncoding("UTF-8");

        R r= R.build(ackCode);
        //r--> json
        String jsonStr= JSONUtil.toJsonStr(r);
        cn.hutool.extra.servlet.ServletUtil.write(response,jsonStr, MediaType.APPLICATION_JSON_VALUE);
    }

    /**
     * 得到HttpServletRequest对象
     * @return
     */
    public static HttpServletRequest getRequest(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) requestAttributes;
        return servletRequestAttributes.getRequest();
    }


    /**
     * 根据键获取请求中的值
     * @param key
     * @return
     */
    public static String getParams(String key){
        return getRequest().getParameter(key);
    }

    /**
     *  得到参数把变整数
     * @param key
     * @return
     */
    public static Integer getParamsToInt(String key){
        String value=getParams(key);
        if(NumberUtil.isInteger(value)){
            return  Integer.parseInt(value);
        }
        else{
            throw new ServiceException(AckCode.SYSTEM_PARAM_FAIL);
        }
    }

    /**
     * 得到参数把变整数
     * @param key
     * @param defaultValue  默认值
     * @return
     */
    public static Integer getParamsToInt(String key,int defaultValue){
        String value=getParams(key);
        if(NumberUtil.isInteger(value)){
            return  Integer.parseInt(value);
        }
        else{
            return defaultValue;
        }
    }

}
